package com.trip_memories;

import java.util.List;

import otherModels.SlideImage;
import signedUser.ChosedAlbum;
import signedUser.Signed;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.ViewFlipper;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import blogic.Operations;

public class SlideShow extends Activity {

	private BusinessLogic bl;
	private List<SlideImage> images;
	private int id = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_slide_show);

		if (savedInstanceState != null)
			id = savedInstanceState.getInt("Number", id);

		bl = AccesBussineLogic.getBussinesLogic(this);

		final ViewFlipper slide = (ViewFlipper) findViewById(R.id.slideShow);

		images = Operations.getPhotos(this, bl, ChosedAlbum.getId(),
				Signed.getId());

		for (SlideImage image : images) {
			slide.addView(image.getImage());
		}

		refreshRestore();

		final Context context = this;

		slide.setOnTouchListener(new OnTouchListener() {

			private float oldx = 0;

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN: {
					oldx = event.getX();
					break;
				}
				case MotionEvent.ACTION_UP: {
					if (event.getX() > oldx + 50) {
						slide.setInAnimation(context, R.anim.fade_in_left);
						slide.setOutAnimation(context, R.anim.fade_out_right);
						slide.showNext();
						id = slide.getDisplayedChild();
						refreshRestore();

					} else if (event.getX() < oldx - 50) {
						slide.setOutAnimation(context, R.anim.fade_out_left);
						slide.setInAnimation(context, R.anim.fade_in_right);
						slide.showPrevious();
						id = slide.getDisplayedChild();
						refreshRestore();
					}
					break;
				}
				}
				return true;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.slide_show, menu);
		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("Number", ((ViewFlipper) findViewById(R.id.slideShow))
				.getDisplayedChild());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		id = savedInstanceState.getInt("Number");
		refreshRestore();
	}

	@Override
	protected void onStart() {
		refreshRestore();
		super.onStart();
	}

	private void refreshRestore() {
		((ViewFlipper) findViewById(R.id.slideShow)).setDisplayedChild(id);
		if (images != null && images.size() > 0)
			((TextView) findViewById(R.id.namePhoto)).setText(images.get(id)
					.getName());
	}

}
